<!--
 * @Description: 
 * @version: 
 * @Author: mikeJang
 * @Date: 2021-07-01 14:10:39
 * @LastEditors: mikeJang
 * @LastEditTime: 2021-08-21 10:57:52
 * @FilePath: \YU_LIN_Frontend\src\components\SDeptSelectSingle.vue
-->
<template>
  <a-select
    size="large"
    show-search
    :filter-option="filterOption"
    :value="value"
    allowClear
    :disabled="disabled"
    placeholder="请选择"
    :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
    :options="departmentList"
    @change="handelChange"
  >
  </a-select>
</template>

<script>

export default {
  name: "SDeptSelectSingle",
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    adcd: {
      type: String,
      required: true,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      departmentList: []
    };
  },
  watch: {
    adcd: {
      handler(val) {
        if (val) {
          this.getList(val);
        } else {
          this.$emit("change", "");
        }
      },
      immediate: true
    }
  },
  methods: {
    // 根据行政区划获取机构
    async getList(val) {
      const params = {
        adcd: val
      }
      await this.$http.userauth.queryDeptByAdcd(params).then(res => {
          if (res) {
            const list = [];
            res.map(item => {
              list.push({
                value: item.id,
                title: item.deptName
              });
            });
            this.departmentList = list;
          } else {
            this.$message.info("当前行政区划下没有部门");
            this.departmentList = [];
          }
        });
      let ids = [];
      if (this.departmentList.length > 0) {
        this.departmentList.map(item => {
          ids.push(item.value);
        });
      } else {
        ids = [];
      }
      if (!ids.includes(this.value)) {
        this.$emit("change", "");
      }
    },
    handelChange(val) {
      this.$emit("change", val);
      this.$emit("change-done", val);
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    }
  }
};
</script>
